<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
*@Name:  Sections Controller
*
*@Author:  Ervin R. Musngi
*
*/

class Sections extends MX_Controller
{
	protected $id = 0;
	protected $validation;
	protected $record_per_page = 0;
	public function __construct()
	{
		parent::__construct();
		
		$this->validation = array(
			array(
				'field'	=> 'name',
				'label'	=> 'Section Name',
				'rules' => 'trim|htmlspecialchars|required|max_length[30]'
			)
		);
		
		$this->load->library('users/auth');
		$this->load->library('form_validation');
		$this->load->library('pagination');
		$this->lang->load('section');
		
		$this->record_per_page = $this->system_library->settings['record_per_page'];
		
		$this->template
				->set_breadcrumb('Home','dashboard')
		   		->set_partial('header', 'partials/header')
				->set_partial('breadcrumb','partials/breadcrumb')
                ->set_partial('footer', 'partials/footer')
				->set_partial('navigation','partials/admin/menu')
                ->set_layout('default')
				->set_theme('default')
				->append_metadata($this->asset->css('reset.css'))
				->append_metadata($this->asset->css('general.css'))
				->append_metadata($this->asset->css('admin/default.css'))
				->append_metadata($this->asset->css('notifcation.slider.css'))
				->append_metadata($this->asset->css('admin/jquery.accordion.css'))
				->append_metadata($this->asset->js('jquery/jquery-1.6.2.min.js'))
				->append_metadata($this->asset->js('jquery.freeow.js'))
				->append_metadata($this->asset->js('notificationBox.jquery.js'))
				->append_metadata($this->asset->js('jquery_accordion_init.js'));
		
	}
	
	public function index()
	{
		$this->listing();
	}
	
	public function listing($page = 0)
	{
		if ($page < 0  || strlen($page) > 13)
		{
			redirect('grading/sections/listing/');
		}
		else
		{
			$limit = $this->record_per_page;
			
			$section = new Section_m();
			
			$total_rows = $section->where('is_active',1)->get(($limit + $page),$page)->count();
			
			if($total_rows < $page)
			{
				redirect('grading/sections/listing/');
			}
			else
			{
				$section
					->where('is_active',1)
					->include_related('school_level',array('id','name'),TRUE,TRUE)
					->where_related('school_level','is_active',1)
					->get($limit,$page);
					
				$data['sections']			= $section;
				
				//setup pagination
				$config['base_url']			= site_url('grading/sections/listing/');
				$config['per_page']			= $limit;
				$config['uri_segment'] 		= 4;
				$config['total_rows']		= $total_rows;
				$config['anchor_class'] 	= 'class="btn"';
				$config['cur_tag_open'] 	= ' <div class="btn btn-disabled">';
				$config['cur_tag_close']	= '</div>';
				$this->pagination->initialize($config);
			
				$data['page'] 			= $this->pagination->create_links();
				
				$this->template
							->append_metadata($this->asset->css('facebox.css'))
							->append_metadata($this->asset->js('jquery/facebox.js'))
							->set_breadcrumb('Sections','')
							->build('section/list',$data);
			}
		}
	}
	
	public function create()
	{
		$this->form_validation->set_rules($this->validation);
		
		if ($this->form_validation->run())
		{
			$section = new Section_m();
			$section->section 				= $this->input->post('name');
			$section->school_level_id	= $this->input->post('school_level');
			$section->save();
			
			$this->session->set_flashdata('msg_title',lang('section_success_title'));
			$this->session->set_flashdata('msg',lang('section_success_insert'));
			redirect('grading/sections/');
		}
		
		$year_level	= new School_level_m();
		
		$year_level->where('is_active',1)->get();
		
		
		foreach ($year_level as $yl)
		{
			$data['year_level'][$yl->id] = $yl->name;
		}
		
		$year_level->sections->get();
		
		$data['sections'] = array();
		
		if (isAjax())
		{
			$this->load->view('section/create',$data);
		}
		else
		{
			$this->template
						->set_breadcrumb('Sections','grading/sections/')
						->set_breadcrumb('create')
						->build('section/create',$data);
		}
	}
	
	public function ajax_get_sections()
	{
		$id = $this->input->post('id',0);
		$school_level = new School_level_m();
		
		$school_level->where('id',$id)->get()->sections->get();
		
		$sections = array();
		
		foreach($school_level->sections as $sl)
		{
			$sections[$sl->id] =  $sl->section;
		}
		
		echo json_encode($sections);
	}
}